package com.car.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class DocUtilProxy {

	static Logger logger = Logger.getLogger(DocUtilProxy.class.getName());
	private Connection conn = ConnectionPool.getConnection("pcauto", "root", "zhouzhou100428", "192.168.1.168");

	public Document getDocProxy(String url, List<String> ipList, int index) {
		String ip[] = ipList.get(index).split(":");
		Document doc = null;
		int i = 0;
		while (i < 2) {
			try {
				doc = Jsoup.connect(url).proxy(ip[0], Integer.parseInt(ip[1])).timeout(30000).get();
				break;
			} catch (Exception e) {
				if (i == 1) {
					saveUrl(url);
					System.out.println(url + "\t" + index);
				}
				i++;
			}
		}
		return doc;
	}

	public void saveUrl(String url) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO error_bbs(url) VALUES(?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, url);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
